#include "options.page.h"
#include "options.dlg.h"

#include <QtWidgets/QtWidgets>

struct OptionsPagePriv {
	OptionsPage* page;

	bool _blocked;
	QString _name;
	QVBoxLayout* _vlayout;
	OptionsPagePriv(OptionsPage* p, const QString&n, const QString& _t):
		page{p},
		_blocked{false}, _name{n}
	{
	_vlayout=new QVBoxLayout{page};
	auto label=new QLabel{_t, page};
	_vlayout->addWidget(label, 0);
	auto line=new QFrame{page};
	_vlayout->addWidget(line, 0);
	line->setFrameShape(QFrame::HLine);
	}
};

OptionsPage::OptionsPage(const QString& n, const QString& _t):
	QWidget{nullptr, 0}, priv{new OptionsPagePriv{this, n, _t}}
{
}
OptionsPage::~OptionsPage() {
	delete priv;
}

QBoxLayout* OptionsPage::layout() const {
	return priv->_vlayout;
}
const QString& OptionsPage::name() const {
	return priv->_name;
}
void OptionsPage::notifyChange() {
	if(!priv->_blocked) {
		bool a, b, c, d;
		getState(&a, &b, &c, &d);
		Q_EMIT stateChanged(a, b, c, d);
	}
}
bool OptionsPage::setBlocked(bool v) {
	auto r=priv->_blocked;
	priv->_blocked=v;
	return r;
}
void OptionsPage::useDefault() {
	auto oldblocked=priv->_blocked;
	priv->_blocked=true;
	useDefaultImp();
	priv->_blocked=oldblocked;
	notifyChange();
}
void OptionsPage::saveDefault() {
	saveDefaultImp();
	notifyChange();
}
void OptionsPage::applyChanges() {
	applyChangesImp();
	notifyChange();
}
void OptionsPage::resetChanges() {
	auto oldblocked=priv->_blocked;
	priv->_blocked=true;
	resetChangesImp();
	priv->_blocked=oldblocked;
	notifyChange();
}
void OptionsPage::setState(SessionState ts) {
	setStateImp(ts);
	notifyChange();
}
